package org.liveboardgames.agricola.view.component
{
    import mx.collections.ArrayCollection;
    import mx.collections.ListCollectionView;
    import mx.containers.Canvas;
    import mx.core.UIComponent;
    import mx.events.CollectionEvent;
    
    import org.liveboardgames.agricola.control.delegates.helper.GameHelper;
    import org.liveboardgames.agricola.model.AgricolaModelLocator;
    import org.liveboardgames.agricola.vo.common.IResourceContainer;
    import org.liveboardgames.agricola.vo.common.Peep;
    import org.liveboardgames.agricola.vo.player.Player;
    import org.liveboardgames.common.dictionary.collections.HashCollection;
    import org.liveboardgames.common.dictionary.collections.IMap;
    
    public class ResourceDisplayer extends Canvas implements IResourceContainer
    {
        public var size:Number;
        
        public var priority:String;
        
        [Bindable]
        private var _resources:HashCollection;
        
        [Bindable]
        private var _peeps:ListCollectionView;
        
        public var allowDrag:Boolean = false;
        
        public function set resources(resources:HashCollection):void
        {
        	if (resources == null)
        	{
        		resources = new HashCollection();
        	}
            _resources = resources;
            cleanResourcesDisplay();
            _resources.addEventListener(CollectionEvent.COLLECTION_CHANGE, onResourcesUpdate);
            onResourcesUpdate(null);
        }
        
        public function get resources():HashCollection
        {
            return this._resources;
        }
        
        public function addResource(resource:String, quantity:int):void
        {
            if (this.resources == null)
            {
                resources = new HashCollection();
            }
            var currentQuantity:int = this.resources.getValue(resource) as int;
            this.resources.put(resource, Math.max(0, currentQuantity + quantity));
        }
        
        public function addResources(resources:IMap):void
        {
            if (resources != null)
            {
                for each (var resource:String in resources.getKeys())
                {
                    var quantity:int = resources.getValue(resource);
                    addResource(resource, quantity);
                }
            }
        }
        
        public function removeResources(resources:IMap):void
        {
            if (this.resources != null && resources != null)
            {
                for each (var resourceType:String in resources.getKeys())
                {
                    var quantity:int = resources.getValue(resourceType);
                    addResource(resourceType, -quantity);
                }
            }
        }
        
        public function set peeps(peeps:ListCollectionView):void
        {
            _peeps = peeps;
            cleanPeepsDisplay();
            if (_peeps != null)
            {
                _peeps.addEventListener(CollectionEvent.COLLECTION_CHANGE, onPeepsUpdate);
                // Initialize the peeps
                onPeepsUpdate(null);
            }
        }
        
        private function onResourcesUpdate(event:Event):void
        {
            cleanResourcesDisplay();
            if (_resources != null)
            {
                for each (var resourceType:String in _resources.getKeys())
                {
                    var quantity:int = _resources.getValue(resourceType) as int;
                    var resourceImage:ResourceImage = new ResourceImage(allowDrag, size);
                    resourceImage.resourceType = resourceType;
                    resourceImage.quantity = quantity;
                    if ("resource" == priority)
                    {
                        this.addChildAt(resourceImage, this.numChildren);
                    }
                    else
                    {
                        this.addChildAt(resourceImage, 0);
                    }
                }
            }
            
            dispatchEvent(new Event("resourcesChanged", true));
        }
        
        private function cleanResourcesDisplay():void
        {
            for each (var child:UIComponent in this.getChildren())
            {
                if (child is ResourceImage)
                {
                    this.removeChild(child);
                }
            }
        }
        
        private function onPeepsUpdate(event:Event):void
        {
            cleanPeepsDisplay();
            var peepsByPlayer:IMap = getPeepsByPlayer(_peeps);
            for each (var owner:Player in peepsByPlayer.getKeys())
            {
                var peepImage:PeepImage = new PeepImage();
                var peeps:ListCollectionView = peepsByPlayer.getValue(owner) as ListCollectionView;
                peepImage.peep = peeps.getItemAt(0) as Peep;
                peepImage.quantity = peeps.length;
                if ("peep" == priority)
                {
                    this.addChildAt(peepImage, this.numChildren);
                }
                else
                {
                    this.addChildAt(peepImage, 0);
                }
            }
        }
        
        private function cleanPeepsDisplay():void
        {
            for each (var child:UIComponent in this.getChildren())
            {
                if (child is PeepImage)
                {
                    this.removeChild(child);
                }
            }
        }
        
        private function getPeepsByPlayer(peeps:ListCollectionView):IMap
        {
            var peepsByPlayer:IMap = new HashCollection();
            for each (var peep:Peep in peeps)
            {
            	var owner:Player = GameHelper.getPeepOwner(AgricolaModelLocator.getInstance().game, peep);
                var peeps:ListCollectionView = peepsByPlayer.getValue(owner) as ListCollectionView;
                if (peeps == null)
                {
                    peeps = new ArrayCollection();
                }
                peeps.addItem(peep);
                peepsByPlayer.put(owner, peeps);
            }
            return peepsByPlayer;
        }
    
    }
}